package cn.exrick.manager.service.impl;

import cn.exrick.common.utils.IDUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class EmailUtil {

    private final static Logger log= LoggerFactory.getLogger(EmailUtil.class);

    @Value("${EMAIL_HOST}")
    private String EMAIL_HOST;

    @Value("${EMAIL_USERNAME}")
    private String EMAIL_USERNAME;

    @Value("${EMAIL_PASSWORD}")
    private String EMAIL_PASSWORD;

    @Value("${EMAIL_SENDER}")
    private String EMAIL_SENDER;

    private JavaMailSenderImpl senderImpl;

    @PostConstruct
    private void init(){
        //设定邮箱服务器配置
        senderImpl = new JavaMailSenderImpl();
        senderImpl.setHost(EMAIL_HOST);
        senderImpl.setUsername(EMAIL_USERNAME);
        senderImpl.setPassword(EMAIL_PASSWORD);
        Properties prop = new Properties();
        //服务器进行认证
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true");

        //不做服务器证书校验
        prop.put("mail.smtp.ssl.checkserveridentity", "false");
        prop.put("mail.smtp.timeout", "20000");
        prop.put("mail.smtp.port", "25");
        senderImpl.setJavaMailProperties(prop);
    }

    @Async
    public void sendEmail(String subject,String sendTo,String content){
        if(StringUtils.isBlank(sendTo)){
            return;
        }

        //发送html邮件
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper;
        //设置邮件内容
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
            messageHelper.setTo(sendTo);
            messageHelper.setFrom(EMAIL_SENDER);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);
            // 发送邮件
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        log.info("用户："+sendTo+" 的邮件发送成功");
    }

    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        //设定邮箱服务器配置
        senderImpl.setHost("uicmail.uic.edu.hk");
        senderImpl.setUsername("uicsouvenir@uic.edu.hk");
        senderImpl.setPassword("Gift3620222");
        Properties prop = new Properties();
        //服务器进行认证
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true");

        //不做服务器证书校验
        prop.put("mail.smtp.ssl.checkserveridentity", "false");
        prop.put("mail.smtp.timeout", "20000");
        prop.put("mail.smtp.port", "25");
        senderImpl.setJavaMailProperties(prop);
        //发送html邮件
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        String title = "纪念品商城注册验证码";
        String code = IDUtil.getOrderCode();
        //设置邮件内容
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
            messageHelper.setTo("1436364196@qq.com");
            messageHelper.setFrom("uicsouvenir@uic.edu.hk");
            messageHelper.setSubject(title);
            String content ="您的验证码是"+code;
            messageHelper.setText(content, true);
            // 发送邮件
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }


}
